{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ONNX Script 急切模式评估\n",
    "onnxscript 函数可以直接作为 Python 函数执行（例如，使用 Python 调试器）。这对于调试 onnxscript 函数定义非常有用。这种执行利用了函数定义中使用的 ONNX 操作的后端实现。目前，后端实现使用 onnxruntime 来执行每个操作调用。这种执行模式被称为急切模式评估。\n",
    "\n",
    "下面的示例说明了这一点。我们首先定义一个 onnxscript 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from onnxscript import FLOAT, script\n",
    "from onnxscript import opset15 as op\n",
    "\n",
    "\n",
    "@script()\n",
    "def linear(A: FLOAT[\"N\", \"K\"], W: FLOAT[\"K\", \"M\"], Bias: FLOAT[\"M\"]) -> FLOAT[\"N\", \"M\"]:  # noqa: F821\n",
    "    T1 = op.MatMul(A, W)\n",
    "    T2 = op.Add(T1, Bias)\n",
    "    Y = op.Relu(T2)\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建用于评估函数的输入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(0)\n",
    "m = 4\n",
    "k = 16\n",
    "n = 4\n",
    "a = np.random.rand(k, m).astype(\"float32\").T\n",
    "w = np.random.rand(n, k).astype(\"float32\").T\n",
    "b = np.random.rand(n).astype(\"float32\").T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "评估结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.7695956 3.8361263 5.116064  5.2047744]\n",
      " [4.5182567 4.2103305 4.54666   5.6752048]\n",
      " [4.0728097 3.1566992 4.821034  4.7809625]\n",
      " [4.925565  3.558134  4.7679787 5.3899584]]\n"
     ]
    }
   ],
   "source": [
    "print(linear(a, w, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xin",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
